﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MSD.WL.Site.Models;

namespace MSD.WL.Site.Controllers
{
    [Authorize]
    public class InvenTypesController : Controller
    {
        private EMSContext db = new EMSContext();

        // GET: InvenTypes
        public ActionResult Index()
        {
            var inventype = db.InvenType as IQueryable<InvenType>;
            var recordCount = inventype.Count();
            var pageCount = GetPageCount(recordCount);

            ViewBag.PageIndex = 0;
            ViewBag.PageCount = pageCount;
            return View(GetPageDataSource(inventype,0,recordCount));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(string inventypecode,string inventypename,int PageIndex)
        {
            var inventype = db.InvenType as IQueryable<InvenType>;
            if (!String.IsNullOrEmpty(inventypecode))
            {
                inventype = inventype.Where(x => x.InvenTypeCode.Contains(inventypecode));
            }
            if (!string.IsNullOrEmpty(inventypename))
            {
                inventype = inventype.Where(x => x.InvenTypeName.Contains(inventypename));
            }

            var recordCount = inventype.Count();
            var pageCount = GetPageCount(recordCount);
            if (PageIndex > pageCount && pageCount >= 1)
            {
                PageIndex = pageCount - 1;
            }
            inventype = inventype.OrderBy(m => m.ID).Skip(PageIndex * PAGE_SIZE).Take(PAGE_SIZE);
            ViewBag.PageIndex = PageIndex;
            ViewBag.PageCount = pageCount;
            return View(inventype.ToList());
        }
        //// GET: InvenTypes/Details/5
        //public ActionResult Details(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    InvenType invenType = db.InvenType.Find(id);
        //    if (invenType == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(invenType);
        //}

        // GET: InvenTypes/Create
        //public ActionResult Create()
        //{
        //    return View();
        //}

        // POST: InvenTypes/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,InvenTypeCode,InvenTypeName")] InvenType invenType)
        {
            if (ModelState.IsValid)
            {
                db.InvenType.Add(invenType);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(invenType);
        }

        // GET: InvenTypes/Edit/5
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    InvenType invenType = db.InvenType.Find(id);
        //    if (invenType == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(invenType);
        //}

        // POST: InvenTypes/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,InvenTypeCode,InvenTypeName")] InvenType invenType)
        {
            if (ModelState.IsValid)
            {
                db.Entry(invenType).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(invenType);
        }

        // GET: InvenTypes/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            InvenType invenType = db.InvenType.Find(id);
            if (invenType == null)
            {
                return HttpNotFound();
            }
            return View(invenType);
        }

        // POST: InvenTypes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            InvenType invenType = db.InvenType.Find(id);
            db.InvenType.Remove(invenType);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        //返回分布视图方法
        [HttpGet]
        public ActionResult GetCreateInvenTypes()
        {
            return PartialView("_CreateInvenTypes");
        }

        [HttpPost]
        public ActionResult GetEditInvenTypes(int id)
        {
            if (id<=0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            InvenType inventype = db.InvenType.Find(id);
            if (inventype == null)
            {
                return HttpNotFound();
            }
            return PartialView("_EditInvenTypes");
        }

        [HttpPost]
        public ActionResult GetDeleteInvenTypes(int id)
        {
            if (id <= 0)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            InvenType inventype = db.InvenType.Find(id);
            if (inventype == null)
            {
                return HttpNotFound();
            }
            return PartialView("_DeleteInvenTypes");
        }

        //根据页面记录大小和数据记录获得页数
        private static readonly int PAGE_SIZE = 20;
        private int GetPageCount(int recordCount)
        {
            int pageCount = recordCount / PAGE_SIZE;
            if(recordCount % PAGE_SIZE != 0)
            {
                pageCount += 1;
            }
            return pageCount;
        }

        //根据分页索引和记录数量得到数据集合
        private List<InvenType> GetPageDataSource(IQueryable<InvenType> inventype,int pageIndex,int recordCount)
        {
            var pageCount = GetPageCount(recordCount);
            if (pageIndex >= pageCount && pageCount >= 1)
            {
                pageIndex = pageCount - 1;
            }
            return inventype.OrderBy(m => m.ID).Skip(pageIndex * PAGE_SIZE).Take(PAGE_SIZE).ToList();
        }
    }
}
